package com.easy.activiti.ext;

import com.easy.activiti.command.RejectCmd;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.springframework.util.Assert;

/**
 * @author zhouchuang
 * 2024-11-05 14:38:40
 */
public class TaskEvent {
    private String processDefinitionKey;
    private String businessKey;
    private TaskEntity taskEntity;
    private String taskId;

    private String processInstanceId;
    private  ProcessEngine processEngine;
    public TaskEvent(String processDefinitionKey,String processInstanceId,String businessKey, TaskEntity taskEntity) {
        this.processDefinitionKey  = processDefinitionKey;
        this.businessKey = businessKey;
        this.taskEntity = taskEntity;
        this.processInstanceId = processInstanceId;
        if(taskEntity!=null){
            this.taskId  = taskEntity.getId();
        }
        // 获取 ProcessEngine 实例
        processEngine = ProcessEngines.getDefaultProcessEngine();
    }
    public TaskEvent(String processDefinitionKey,String processInstanceId, String businessKey) {
        this.processDefinitionKey = processDefinitionKey;
        this.businessKey = businessKey;
        this.processInstanceId = processInstanceId;
    }
    public String getBusinessKey() {
        return businessKey;
    }

    public String getProcessDefinitionKey() {
        return processDefinitionKey;
    }


    public String getTaskId() {
        return taskId;
    }

    public ProcessEngine getProcessEngine() {
        return processEngine;
    }

    public TaskEntity getTaskEntity() {
        return taskEntity;
    }
    public void setAssignee(String assignee){
        Assert.notNull(taskEntity,"不是任务节点不能设置处理人");
        processEngine.getTaskService().setAssignee(taskEntity.getId(),assignee);
    }
    public void reject(){
        RejectCmd rejectCmd = new RejectCmd(taskEntity.getProcessInstanceId());
        processEngine.getManagementService().executeCommand(rejectCmd);
    }

    public String getProcessInstanceId() {
        return processInstanceId;
    }
}
